//index.js
var utils = require("../../../utils/utils.js");
//获取应用实例
const app = getApp()
Page({
  data: {
    titleTop: app.globalData.navTop,
    titleHeight: app.globalData.menuButtonHeight,
    balance: '0.00',
    list: ['5', '10', '15', '20', '25', '30'],
    changeIndex: '',
    chooseIndex: 0,
    firstOpen: 0,
    rechargeOrder: '',
    setInter: '',
    showBtn: true
  },
  changeImg(e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      chooseIndex: index
    })
  },
  changeIndex(e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      changeIndex: index
    })
    wx.setStorageSync('rechargeMoey', index)
  },
  goUrlW(e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: '/pages/my/manualDetail/manualDetail?code=' + url + '&name=充值协议&iscode=1',
    })
  },
  //我要充值
  getIndex() {
    var that = this;
    utils.request('recharge/rechargeAmountLists', {}, function (res) {
      that.setData({
        list: res.data.data.lists,
        balance: utils.returnFloat(res.data.data.userBalance),
        changeIndex: wx.getStorageSync('rechargeMoey') ? wx.getStorageSync('rechargeMoey') : res.data.data.lists[0].id
      })
    })
  },
  goHome() {
    wx.navigateBack()
  },
  btnConfirm() {
    var that = this;
    if (that.data.chooseIndex == 0) {
      utils.toast('请您仔细阅读并同意充值协议');
      return false;
    }
      that.selectComponent("#loading").showLoadingCus('请稍候');
    that.setData({
      showBtn: false
    })
    utils.request('recharge/createOrder', { 'rechargeAmountId': that.data.changeIndex, 'paymentMethod': '1', orderNo: '' }, function (res) {
      that.setData({
        rechargeOrder: res.data.data.orderNo
      })
      if (utils.is_define(res.data.data.payInfo)) {
        wx.hideLoading()
        qq.requestWxPayment({
          url: res.data.data.payInfo,
          referer: res.data.data.referer,
          success(res) {
            that.setData({
              showBtn: true
            })
            that.setZF();;
          },
          fail(res) {
             that.selectComponent("#loading").hideLoadingCus();
            that.setData({
              showBtn: true
            })
            utils.toast('支付失败，请重试')
          }
        })
      } else {
         that.selectComponent("#loading").hideLoadingCus();
        that.setData({
          showBtn: true
        })
        utils.toast(res.data.data.msg)
      }
    })
  },
  //查询支付
  setZF() {
    var that = this;
    var num = 1;
    that.data.setInter = setInterval(function () {
       num++;
      utils.request('recharge/queryOrder/' + that.data.rechargeOrder, {}, function (res) {
        if (res.data.code == 200) {
          if (res.data.data.orderStatus != 1) {
            clearInterval(that.data.setInter)
            that.selectComponent("#loading").hideLoadingCus();
            that.setpRICE();
          } else {
            if(num==5){
               clearInterval(that.data.setInter)
                 that.selectComponent("#loading").hideLoadingCus();
                utils.toast('支付失败，请重试')
            }
          }
        } else {
           clearInterval(that.data.setInter)
           that.selectComponent("#loading").hideLoadingCus();
          utils.toast(res.data.msg)
        }
      })
    }, 1000)

  },
  setpRICE() {
    var that = this;
    app.get_local('AccToken').then(res => {
      if (utils.is_define(res)) {
        utils.request('user/userBalance', {}, function (res) {
          if (res.data.code == 200) {
            that.setData({
              balance:res.data.data.data,
            })
             wx.navigateTo({
                url: '/pages/my/rechargeSuccess/rechargeSuccess?orderNo='+that.data.rechargeOrder,
              })
          } else {
            utils.toast(res.data.msg)
          }
        })
      }
    })
  },
  dyxx() {
    var that = this;
    wx.requestSubscribeMessage({
      tmplIds: ['9a_bdGaz1Ex6YxtHyRrck3icHrqmUnpk55gPRa5nGug'],
      success(res) {
        utils.request('Member/setPush', { status: JSON.stringify(res) }, function (res) {
          wx.navigateTo({
            url: '../rechargeSuccess/rechargeSuccess',
          })
        })
      },
      fail(err) {
        wx.navigateTo({
          url: '../rechargeSuccess/rechargeSuccess',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getIndex();
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onUnload: function () {
    wx.removeStorageSync('rechargeMoey')
    console.log(wx.getStorageSync('rechargeMoey'))
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (that.data.firstOpen == 1) {
      that.getIndex();
    } else {
      that.setData({
        firstOpen: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})